<?php

namespace App\Http\Middleware;

use App\User;
use App\Model\Version;
use Closure;

class CheckApi
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $uid = request()->header('uid');
        $token = request()->header('token');
        //版本号
        $version = request()->header('version');
        if (!User::where('id', $uid)->where('token', $token)->first()) {
            return ['code'=>2,'msg'=>'重新登陆'];
        }
        //版本信息
        //默认权限
        $defArr = ['api/group/is_buy_sure','api/group/is_buy','api/group/order','api/group/order_de'];
        if($version){
            //当前最新版本
            $ver = Version::where('type', 1)->latest('id')->first();
            if($version < $ver['version_id']){
                //当前路径
                $path = $request->path();
                if(in_array($path,$defArr)){
                    return ['code'=>0,'msg'=>'当前版本不是最新版，请更新后再使用'];
                }
            }
        }
        $request['uid'] = $uid;
        $request['token'] = $token;

        return $next($request);
    }
}
